# LDA 分类 iris 分析例子
# 采用交叉验证
# 并调试超参数
# 没用，n只能取1或2
import matplotlib.pyplot as plt
from sklearn import datasets
import numpy as np
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA

from sklearn import metrics
import pandas
import seaborn as sn

### 加载数据  ########################################
iris = datasets.load_iris()
# print(iris)
X = iris.data
y = iris.target
target_names = iris.target_names
# print(X)
# print(y)

### 数据分析 ##########################################
# print(X.shape, y.shape)
# z = y.reshape(-1, 1)
# data = np.hstack((X, z))
# data = pandas.DataFrame(data)
# data.rename(columns={0: "萼片长", 1: "萼片宽", 2: "花瓣长", 3: "花瓣宽", 4: "种类"}, inplace=True)
# # data.head()
# kind_dict = {0: "山鸢尾", 1: "杂色鸢尾", 2: "维吉尼亚鸢尾"}
# data["种类"] = data["种类"].map(kind_dict)
# sn.set_style('white', {'font.sans-serif': ['simhei', 'Arial']})
# sn.pairplot(data, hue="种类")

### 调用LDA 求解，这里采用2维投影 ###########################
# from sklearn.model_selection import KFold
# kf = KFold(n_splits=5) # 5折
# for train_index, test_index in kf.split(X):
#
#     x_train, y_train = X[train_index], y[train_index]
#     x_test, y_test = X[test_index], y[test_index]

from sklearn.model_selection import cross_val_score

acc = np.zeros((2, 1))
for k in range(1, 3):
    lda = LDA(n_components=k)   #
    scores = cross_val_score(lda, X, y, cv=4)
    print(scores)
    acc[k-1] = np.average(scores)

print(acc)
plt.plot(acc)


plt.show()
